{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 矩阵：\n",
    "- 标量 ： 0维\n",
    "- 向量 ： 1维\n",
    "- 矩阵 ： 2维\n",
    "- 张量 ： 多维 >= 3维\n",
    "\n",
    "# 特殊矩阵：\n",
    "- 0矩阵\n",
    "- 单位矩阵：对角线上1，其他为0\n",
    "- 对角矩阵：对角线上的元素为非0，其他为0\n",
    "- 对称矩阵：矩阵的转置等于矩阵自身 A = A.T"
   ],
   "id": "6d254ed80a0e4600"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:16:45.517663Z",
     "start_time": "2025-09-04T13:16:45.506585Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 特殊矩阵\n",
    "# 1.13模块编程.12package. 单位矩阵\n",
    "import numpy as np\n",
    "arr = np.eye(3) # 参数3 ，说明3X3矩阵\n",
    "print(arr)\n",
    "\n",
    "arr = np.eye(3,4) # 3X4矩阵\n",
    "print(arr)\n",
    "\n",
    "arr = np.eye(3,dtype= str)\n",
    "print(arr)"
   ],
   "id": "a766da834368f33d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]]\n",
      "[['1' '' '']\n",
      " ['' '1' '']\n",
      " ['' '' '1']]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:19:10.555161Z",
     "start_time": "2025-09-04T13:19:10.547921Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 对角矩阵：主对角上非0，其他元素为0\n",
    "arr = np.diag([1,2,3,4]) # 创建4X4矩阵，1.13模块编程.12package，2，3，4代表主对角线上的元素\n",
    "print(arr)\n",
    "\n",
    "arr = np.diag([4]) # 创建1X1矩阵,4代表主对角线上的元素\n",
    "print(arr)"
   ],
   "id": "80f5682a281207ed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0]\n",
      " [0 2 0 0]\n",
      " [0 0 3 0]\n",
      " [0 0 0 4]]\n",
      "[[4]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:23:28.415915Z",
     "start_time": "2025-09-04T13:23:28.409127Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机数组的生成\n",
    "# 生成0到1之间的随机浮点数（均匀分布）\n",
    "arr = np.random.rand(2,3)\n",
    "print(arr)\n",
    "\n",
    "# 生成指定范围的随机浮点数\n",
    "arr = np.random.uniform(3, 6, (2,3)) # 创建2X3矩阵，3到6之间的随机浮点数\n",
    "print(arr)\n",
    "\n",
    "# 生成指定范围的随机整数\n",
    "arr = np.random.randint(3, 10, (2,3)) # 创建2X3矩阵，3到10之间的随机整数\n",
    "print(arr)"
   ],
   "id": "aaa72b9733ba2de6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.79457977 0.94743126 0.10524196]\n",
      " [0.71140695 0.26950537 0.05785722]]\n",
      "[[5.88417965 5.53709206 3.96014322]\n",
      " [4.76518737 5.90056873 4.7504303 ]]\n",
      "[[8 6 6]\n",
      " [7 5 8]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:28:09.693377Z",
     "start_time": "2025-09-04T13:28:09.686354Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成随机数列 （正态分布）例如：两边小（概率小），中间大（密度大，概率大） -3到3之间\n",
    "arr = np.random.randn(2,3) # 创建2X3矩阵，正态分布\n",
    "print(arr)"
   ],
   "id": "96b19debd3992085",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.01793004 -1.78922545 -1.40298835 -1.44395221  0.39557556]\n",
      " [-0.08653274 -1.14475013  0.62937509  0.23339157 -0.71635585]\n",
      " [-0.75017749 -1.74564616  0.00435157  0.15320697 -0.08378358]\n",
      " [ 1.28264087  1.36376138  1.01802753 -0.5469255   2.54442243]\n",
      " [ 2.12600409 -1.06307434 -1.42354864 -1.09345806 -0.25030039]]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 如何理解均匀分布和正态分布\n",
    "### 均匀分布：概率密度函数为常数，概率密度函数为f(x) = 1/(b-a)\n",
    "### 正态分布：概率密度函数为高斯分布，概率密度函数为f(x) = 1/sqrt(2*pi*sigma^2) * exp(-(x-mu)^2/2*sigma^2)"
   ],
   "id": "d91873b3d23956b6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:30:03.023050Z",
     "start_time": "2025-09-04T13:30:03.015345Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置随机种子\n",
    "np.random.seed(2)\n",
    "arr = np.random.randint(1,10,(2,5))\n",
    "print(arr)\n",
    "# 随机种子有何作用？ 双方可以设置同样的种子，可以达到生成的随机数组元素保持一样"
   ],
   "id": "e1ca103755c10e3a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 9 7 3 9]\n",
      " [8 3 2 6 5]]\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# numpy-ndarray 的数据类型\n",
    "- 布尔类型 bool\n",
    "- 整数类型 int8, int16, int32, int64, uint8, uint16, uint32, uint64\n",
    "- 浮点类型 float16, float32, float64\n",
    "- 复数类型 complex64, complex128\n",
    "\n",
    "1. 创建数组时，numpy会自动将数据类型转换成最小的数据类型\n",
    "2. 布尔类型-bool，整数类型-int/uint，浮点类型-float/complex"
   ],
   "id": "d744156b60a84eff"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:40:55.485497Z",
     "start_time": "2025-09-04T13:40:55.478938Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,0,3,0,5], dtype=np.bool)\n",
    "print(arr)\n",
    "\n",
    "arr = np.array([1,0,3,0,127], dtype=np.int8) # in8: [-128,128)\n",
    "print(arr)"
   ],
   "id": "a7406b832fe3705a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False  True False  True]\n",
      "[  1   0   3   0 127]\n"
     ]
    }
   ],
   "execution_count": 122
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
